package com.ymttest.database.mongo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mongodb.MongoClientURI;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

public class SmsRecordWapper {
	MongoDBDriver mdd;

	public SmsRecordWapper() throws Exception {
		 Date currentTime = new Date();
		   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		   String dateString = formatter.format(currentTime); 
		mdd = new MongoDBDriver(new MongoClientURI(EnvSetup.getSystemUrl("set5smsmongourl")));
		Logger.debug(mdd.getDb().getMongo().getUsedDatabases());
		mdd.selectCollection("SmsRecord-"+dateString);
	}

	/**
	 * 用appid,Content,Phone查询整条记录
	 * 
	 * @param appid, content, phone
	 * @return SmsRecordBean
	 * @throws Exception
	 */
	public SmsRecordBean getCollectionByAppIdContentPhones(String appid, String content, List<String> phones) throws Exception {
		String re = null;
		SmsRecordBean bean = null;
		
		String phone="";
		for (String string : phones) {
			phone+="\""+string+"\",";
		}
		phone=phone.substring(0,phone.length()-1);
		
		String filter="{\"AppId\":\"" + appid + "\",\"Content\":\"" + content + "\",\"Phones\":[" + phone + "]}";
		Logger.debug("filter:"+filter);
		ArrayList<String> list = mdd.selectPart(filter);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				bean = (SmsRecordBean) FormatUtil.GSONToObject(re, SmsRecordBean.class);
				return bean;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public SmsRecordBean getCollectionById(String id) throws Exception {
		String re = null;
		SmsRecordBean bean = null;
		
		String filter="{\"_id\":\"" + id + "\"}";
		Logger.debug("filter:"+filter);
		ArrayList<String> list = mdd.selectPart(filter);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				bean = (SmsRecordBean) FormatUtil.GSONToObject(re, SmsRecordBean.class);
				return bean;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
